在電腦系統領域中, 應用層級的併發 是指刻意重疊邏輯控制流程以提升效能和回應速度。這是一種功能性的抽象:將程式分割成可獨立執行的任務,這些任務可以交錯執行或並行處理。
1. 併發的分類
開發者通常在三種基本機制之間選擇,以管理這些併發流程:
- 流程: 具有高度隔離性,擁有獨立的記憶體空間;需透過核心中介的通訊機制(IPC)。
- I/O 多工: 單一流程手動切換至「就緒」事件(狀態機)。
- 執行緒: 輕量級流程共享單一虛擬記憶體空間,以便於資料交換。
2. 虛擬執行與實際執行
雖然所有 平行程式 都是併發的,但並非所有併發程式都是平行的。平行性是指在不同硬體核心上實際執行流程。併發則是允許這種執行發生的邏輯設計。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which concurrency model offers the highest level of isolation between flows?
Threads
I/O Multiplexing
Processes
Coroutines
✅ Correct!
Processes have separate virtual address spaces, making them highly isolated but requiring IPC for communication.❌ Incorrect
Threads share the same address space, and I/O multiplexing runs in a single process flow.QUESTION 2
A program is 'Parallel' only if its concurrent flows...
...are managed by the kernel.
...execute simultaneously on separate processor cores.
...use shared memory for communication.
...are triggered by I/O events.
✅ Correct!
Parallelism is a subset of concurrency defined by physical simultaneity on hardware.❌ Incorrect
Logical concurrency can happen on a single core through time-slicing; parallelism requires multiple cores.QUESTION 3
What is a primary disadvantage of I/O Multiplexing compared to Threads?
High context-switch overhead.
Inability to exploit multi-core parallelism.
Difficulty in sharing global data.
Requirement for complex IPC mechanisms.
✅ Correct!
I/O multiplexing uses a single logical flow, meaning it cannot run tasks in parallel across multiple CPUs.❌ Incorrect
I/O multiplexing actually has lower overhead than threads and shares data easily since it's a single process.QUESTION 4
Why do modern windowing systems use concurrency for UI responsiveness?
To ensure memory safety.
To allow the interface to respond to user input while background tasks execute.
To prevent the kernel from reaping child processes.
To bypass the need for a file descriptor table.
✅ Correct!
Concurrency allows the main event loop to remain free for user interactions (clicks/resizing) while other flows handle computation.❌ Incorrect
Responsiveness is about overlapping the 'waiting' for human input with actual work.QUESTION 5
In the taxonomy of concurrency, which model models the application as a state machine?
Process-based
Thread-based
I/O Multiplexing
Sequential programming
✅ Correct!
I/O multiplexing often uses the 'select' or 'epoll' functions to transition between states based on file descriptor readiness.❌ Incorrect
Process and thread models usually follow a sequential-like flow within each independent entity.Deep Dive: Concurrency Architecture & Synchronization
Analysis of Process Management and Buffer Safety
Examine the mechanics of a concurrent server using process-based concurrency and the synchronization requirements of shared buffers in producer-consumer systems.
Q
Figure 12.5 demonstrates a concurrent server in which the parent process creates a child process to handle each new connection request. Trace the value of the reference counter for the associated file table for Figure 12.5.
Solution:
The reference counter for the connected file descriptor table entry evolves as follows:
1. Initial: The parent accepts a connection, creating
2. Fork: The parent calls
3. Parent Close: The parent calls
4. Child Termination: The child handles the request and exits (or calls
The reference counter for the connected file descriptor table entry evolves as follows:
1. Initial: The parent accepts a connection, creating
connfd. Reference count = 1.2. Fork: The parent calls
fork(). The child inherits the descriptor table. Reference count = 2.3. Parent Close: The parent calls
Close(connfd). Reference count = 1.4. Child Termination: The child handles the request and exits (or calls
Close). Reference count = 0, allowing the kernel to reclaim the socket.Q
Let $p$ denote producers, $c$ consumers, and $n$ the buffer size. Is the mutex semaphore in sbuf_insert/sbuf_remove necessary if (Scenario 1) $p=1, c=1$ and (Scenario 2) $p=1, c=10$?
Solution:
Scenario 1 ($p=1, c=1$): In a basic circular buffer where the producer only modifies the 'rear' and the consumer only modifies the 'front', a mutex is not strictly necessary for correctness, though often used for simplicity.
Scenario 2 ($p=1, c=10$): Yes, the mutex is mandatory. Because multiple consumers are competing to update the 'front' index and decrement the item count, a race condition would occur without mutual exclusion.
Scenario 1 ($p=1, c=1$): In a basic circular buffer where the producer only modifies the 'rear' and the consumer only modifies the 'front', a mutex is not strictly necessary for correctness, though often used for simplicity.
Scenario 2 ($p=1, c=10$): Yes, the mutex is mandatory. Because multiple consumers are competing to update the 'front' index and decrement the item count, a race condition would occur without mutual exclusion.
Q
If the parent process in Figure 12.5 failed to close its copy of the connected descriptor, what specific resource leak occurs?
Solution:
This results in a file descriptor leak. Even if the child process exits and closes its copy, the reference count in the kernel's file table never reaches zero because the parent (which runs forever) still holds a reference. Eventually, the server will hit its limit for open files and fail to accept new connections.
This results in a file descriptor leak. Even if the child process exits and closes its copy, the reference count in the kernel's file table never reaches zero because the parent (which runs forever) still holds a reference. Eventually, the server will hit its limit for open files and fail to accept new connections.